package com.baichang.taoshunda.controller;

import com.baichang.framework.controller.IController;
import com.baichang.framework.controller.IPageController;
import com.baichang.framework.controller.WebBaseController;
import com.baichang.framework.excelption.ServiceException;
import com.baichang.framework.model.Page;
import com.baichang.taoshunda.ControllerConstants;
import com.baichang.taoshunda.service.ScoreGoodsService;
import com.baichang.taoshunda.service.ScoreListService;
import com.baichang.taoshunda.service.ScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;

/**
 * Created by Administrator on 2017/8/23 0023.
 */
@Controller
@RequestMapping(ControllerConstants.SCORE)
public class ScoreWController extends WebBaseController {


    @Autowired
    private ScoreGoodsService scoreGoodsService;
    @Autowired
    private ScoreListService scoreListService;
    @Autowired
    private ScoreService scoreService;

    /**
     * 跳转商品积分页面
     *
     * @return
     */
    @RequestMapping("/findScoreIndex")
    public String findScore() {
        return "taoshunda/score/goodsScore";
    }

    /**
     * 跳转商品积分页面
     *
     * @return
     */
    @RequestMapping("/findUserScoreIndex")
    public String findUserScore() {
        return "taoshunda/score/userScore";
    }

    /**
     * 跳转商品积分页面
     *
     * @return
     */
    @RequestMapping("/findExchangScoreIndex")
    public String findExchangeScore() {
        return "taoshunda/score/exchangeScore";
    }

    /**
     * 查询商品积分
     *
     * @param request
     * @param response
     */
    @RequestMapping("/queryGoodsScore")
    public void queryGoodsScore(final HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                String name = getString(request, "name", "");
                return scoreGoodsService.queryGoodsScore(name, nowPage, pageSize);
            }
        });
    }

    /**
     * 添加积分商品
     *
     * @param request
     * @param response
     */
    @RequestMapping("/addGoodsScore")
    public void addGoodsScore(final HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                String goodsScore = getString(request, "goodsScore", "");
                String hotLine = getString(request, "hotLine", "");
                String needScore = getString(request, "needScore", "");
                BigDecimal marketPrice = BigDecimal.valueOf(getDouble(request, "marketPrice", 0));
                Integer stock = getInt(request, "stock", 0);
                String goodsPic = getString(request, "goodsPic", "");
                String listPic = getString(request, "listPic", "");
                String remark = getString(request, "remark", "");
                return scoreGoodsService.addGoodsScore(goodsScore, hotLine, needScore, marketPrice, stock, goodsPic, listPic, remark);
            }
        });
    }

    /**
     * 修改积分商品
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updateGoodsScore")
    public void updateGoodsScore(final HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer id = getInt(request, "id", 0);
                String goodsScore = getString(request, "goodsScore", "");
                String hotLine = getString(request, "hotLine", "");
                String needScore = getString(request, "needScore", "");
                BigDecimal marketPrice = BigDecimal.valueOf(getDouble(request, "marketPrice", 0));
                Integer stock = getInt(request, "stock", 0);
                String goodsPic = getString(request, "goodsPic", "");
                String listPic = getString(request, "listPic", "");
                String remark = getString(request, "remark", "");
                return scoreGoodsService.updateGoodsScore(id, goodsScore, hotLine, needScore, marketPrice, stock, goodsPic, listPic, remark);
            }
        });
    }

    /**
     * 删除积分商品
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updateState")
    public void updateState(final HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer id = getInt(request, "id", 0);
                Integer state = getInt(request, "state", 0);
                return scoreGoodsService.updateState(id, state);
            }
        });
    }

    /**
     * 兑换积分商品历史
     *
     * @param request
     * @param response
     */
    @RequestMapping("/queryHistoryGoodsScore")
    public void queryHistoryGoodsScore(final HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                String name = getString(request, "name", "");
                String startCreate = getString(request, "startCreate", "");
                String endCreate = getString(request, "endCreate", "");
                String account = getString(request, "account", "");
                return scoreListService.queryHistoryGoodsScore(name, startCreate, endCreate, account, nowPage, pageSize);
            }
        });
    }

    /**
     * 查询用户积分
     *
     * @param request
     * @param response
     */
    @RequestMapping("/queryUserScore")
    public void queryUserScore(final HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                String name = getString(request, "name", "");
                return scoreListService.queryUserScore(name, nowPage, pageSize);
            }
        });
    }

    /**
     * 送货
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updateIsSendGoods")
    public void updateIsSendGoods(final HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer id = getInt(request, "id", 0);
                Integer sendGoods = getInt(request, "sendGoods", 0);
                return scoreGoodsService.updateIsSendGoods(id, sendGoods);
            }
        });
    }
}
